#include "GlobalPrec.h"

void GameLayerConfigElem::MTLoad(CCNode* parent)
{
	InterfaceLoadTask *task=new InterfaceLoadTask;
	task->SetParent(parent);
	task->mConfig=this;
	GTask->AddTask(task);
}

CCNode* GameLayerConfigLayer::Create()
{
	GameLayer* layer = GameLayerConfigMgr::Instance()->ElemCreator<GameLayer>(mName);
	layer->autorelease();
	layer->setContentSize(CCSize(mWidth,mHeight));
	int x=mPosX,y=mPosY;
	if(mXT) x=mWidth-x;
	if(mYT) y=mHeight-y;
	layer->setPosition(x,y);
	layer->SetZOrder(mZOrder);
	layer->setAnchorPoint(ccp(0,0));

	for(CSList<GameLayerConfigElem*>::iterator iter=mSubElemLst.Begin();iter!=mSubElemLst.End();++iter)
	{
		GameLayerConfigElem* elem = *iter;
		elem->MTLoad(layer);
		/*CCNode* anode = elem->Create();
		layer->AddChild(elem,anode);*/
	}

	if(!layer->init())
		return 0;

	return layer;
}

CCNode* GameLayerConfigButton::Create()
{
	CCMenuButton* button = CCMenuButton::Create();

	button->init(this);
	button->setAnchorPoint(ccp(0,0));
	CCSize size = button->getContentSize();
	if(mWidth!=0) size.width=mWidth;
	if(mHeight!=0) size.height=mHeight;
	button->setContentSize(size);
	int x=mPosX,y=mPosY;
	if(mXT) x=mWidth-x;
	if(mYT) y=mHeight-y;
	button->setPosition(ccp(x,y));
	return button;
}

CCNode* GameLayerConfigSprite::Create()
{
	CCSprite* sprite = CCSprite::create(*mImage);
	CCSize size = sprite->getContentSize();
	if(mWidth!=0) size.width=mWidth;
	if(mHeight!=0) size.height=mHeight;
	sprite->setContentSize(size);
	int x=mPosX,y=mPosY;
	if(mXT) x=mWidth-x;
	if(mYT) y=mHeight-y;
	sprite->setPosition(ccp(x,y));
	sprite->setAnchorPoint(ccp(0,0));
	return sprite;
}

InterfaceLoadTask::InterfaceLoadTask()
{
	mParent=NULL;
	mConfig=NULL;
}

InterfaceLoadTask::InterfaceLoadTask(InterfaceLoadTask &other)
{
	SetParent(other.mParent);
	mConfig=other.mConfig;
}

InterfaceLoadTask::~InterfaceLoadTask() 
{
	if(mParent)
		mParent->release();
}

void InterfaceLoadTask::DoTask()
{
	CCNode* newnode = mConfig->Create();
	GameLayer* layer = dynamic_cast<GameLayer*>(mParent);
	if(layer)
		layer->AddChild(mConfig,newnode);
	else
		mParent->addChild(newnode,mConfig->mZOrder);

}
void InterfaceLoadTask::SetParent(CCNode* parent) 
{
	mParent=parent;
	if(mParent) 
		mParent->retain();
}



